﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Control_SelYear : System.Web.UI.UserControl
{
    private int _StartOffsetYear = 2;
    private int _EndOffsetYear = 2;
    private bool _AddNeedSeldate = true;
    private string _SelectYear = "";
   
    /// <summary>
    /// 显示起始的时间与现在的偏离值
    /// </summary>
    public int StartOffsetYear
    {
        get
        {
            return _StartOffsetYear;
        }
        set
        {
            _StartOffsetYear = value;
        }
    }

    /// <summary>
    /// 显示终止的时间与现在的偏离值
    /// </summary>
    public int EndOffsetYear
    {
        get
        {
            return _EndOffsetYear;
        }
        set
        {
            _EndOffsetYear = value;
        }
    }

    /// <summary>
    /// 当前选择的时间年值
    /// </summary>
    public string SelectYear
    {
        get
        {
            return SelYear.SelectedValue;
        }
        set
        {
            _SelectYear = value;
            if (SelYear.Items.FindByValue(_SelectYear) != null) SelYear.SelectedValue = _SelectYear;
        }
    }

    /// <summary>
    /// 控件是否可操作
    /// </summary>
    public bool Enabled
    {
        get
        {
            return SelYear.Enabled;
        }
        set
        {
            SelYear.Enabled = value;
          }
    }

    /// <summary>
    /// 是否添加'请选择'选项
    /// </summary>
    public bool AddNeedSeldate
    {
        get
        {
            return _AddNeedSeldate;
        }
        set
        {
            _AddNeedSeldate = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            FillData();
        }
        else
        {
            if (SelYear.Items.Count == 0)
            {
                FillData();
            }
        }
    }

    /// <summary>
    /// 填仓数据
    /// </summary>
    private void FillData()
    {
        //  add the first yaer
        if (_AddNeedSeldate == true)
        {
            ListItem dt = new ListItem("请选择", "0");
            SelYear.Items.Add(dt);
        }

        int FirstYear = DateTime.Now.Year;
        FirstYear = FirstYear - _StartOffsetYear;
        int LastYear = FirstYear + _StartOffsetYear + _EndOffsetYear;

        for (int i = FirstYear; i <= LastYear; i++)
        {
            ListItem dt1 = new ListItem(i.ToString().Trim() + "年", i.ToString().Trim());
            SelYear.Items.Add(dt1);
        }
        if (_SelectYear.Length == 0)
        {
            if (_AddNeedSeldate == true)
            {
                _SelectYear = "0";
            }
            else
            {
                _SelectYear = DateTime.Now.Year.ToString();
            }
        }

        if (SelYear.Items.FindByValue(_SelectYear) != null) SelYear.SelectedValue = _SelectYear;
    

    }
}
